package com.yjf.ljr.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.UpdateInfoDataClass;
import com.yjf.ljr.dataclass.UpdateInfoDataClass.UpdateInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;

public class MoreActivity extends IjActivity implements OnClickListener {

	/** 初始化客户中心 **/
	@ID("llAboutUs")
	// 关于我们
	private LinearLayout mLlAboutUs;
	@ID("llHelpCenter")
	private LinearLayout mLlHelpCenter;
	@ID("llNews")
	private LinearLayout mLlNews;
	@ID("llUpdate")
	private LinearLayout mLlUpdate;
	@ID("llCustomPhone")
	private LinearLayout mLlCustomPhone;
	@ID("tvArrowUpdate")
	private TextView mTvArrowUpdate;
	@ID("tvArrowPhone")
	private TextView mTvArrowPhone;
	@ID("btnShare")
	private TextView mBtnShare;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepg_invest_person);

		initControls();
	}

	/** 初始化控件 **/
	private void initControls() {
		findViewById(R.id.btn_title_left).setVisibility(View.GONE);
		setTitleStr("更多");

		mTvArrowUpdate.setText("V" + AppUtil.getVersionName(this));
		mLlAboutUs.setOnClickListener(this);
		mLlHelpCenter.setOnClickListener(this);
		mLlNews.setOnClickListener(this);
		mLlUpdate.setOnClickListener(this);
		mLlCustomPhone.setOnClickListener(this);
		mBtnShare.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 客户中心
		case R.id.llAboutUs: // 关于我们
			startActivity(new Intent(this, AboutUsActivity.class));
			break;
		case R.id.llHelpCenter: // 帮助中心
			startActivity(new Intent(this, HelpActivity.class));
			break;
		case R.id.llNews: // 升级
			startActivity(new Intent(this, NewsActivity.class));
			break;
		case R.id.llUpdate: // 升级
			showProgressDialog();
			new CheckUpdateTask().execute();
			break;
		case R.id.llCustomPhone: // 客户电话
			AppUtil.callNumber(this, mTvArrowPhone.getText().toString());
			break;
		case R.id.tv_title_right: // 登录
			loginWebview();
			break;
		case R.id.btnShare:
			break;
		}
	}

	/** 检查升级 **/
	class CheckUpdateTask extends AsyncTask<Void, Void, String> {
		private UpdateInfoDataClass dc = new UpdateInfoDataClass();

		@Override
		protected String doInBackground(Void... params) {
			RequestObject reqObj = new RequestObject();
			reqObj.method = "appUpdateInfo";
			reqObj.map.put("verCode", "" + AppUtil.getVersionCode(MoreActivity.this));
			reqObj.map.put("verName", AppUtil.getVersionName(MoreActivity.this));
			reqObj.map.put("channelCode", AppUtil.getChanel(MoreActivity.this));
			reqObj.map.put("type", "ANDROID");
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				if (dc.updateMessage != null) {
					UpdateInfo info = dc.updateMessage;
					if (info.optionalUpdateCode <= AppUtil.getVersionCode(MoreActivity.this)
							&& AppUtil.getVersionCode(MoreActivity.this) >= info.forceUpdateCode) {
						showToast("当前已是最新版本！");
					} else {
						showUpdateDialog(info);
					}
				}
			}
			dismissProgressDialog();
		}
	}

	public void showUpdateDialog(final UpdateInfo updateInfo) {

		if (updateInfo == null) {
			return;
		}
		final Dialog dialog = new Dialog(mContext, R.style.dialog);
		dialog.setContentView(R.layout.update_dialog);
		LayoutParams lay = dialog.getWindow().getAttributes();
		setParams(lay);

		TextView tvHint = (TextView) dialog.findViewById(R.id.tvHint_update);
		TextView tvtitle = (TextView) dialog.findViewById(R.id.tv_title);
		Button btnTitleLeft = (Button) dialog.findViewById(R.id.btn_title_left);
		btnTitleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.cancel();
			}
		});
		tvtitle.setText("版本升级");
		TextView tvOldVersion = (TextView) dialog.findViewById(R.id.tvOld_version);
		TextView tvHintUpdateTitle = (TextView) dialog.findViewById(R.id.tvHint_update_title);

		Button btnUpdate = (Button) dialog.findViewById(R.id.btnLogin_update);
		// Button mBtnNextTime = (Button)
		// dialog.findViewById(R.id.btnCancel_update);
		// Button closeBtn = (Button) dialog.findViewById(R.id.btnback_update);
		// if (!("-1".equals((AppUtil.getVersionCode(MoreActivity.this) +
		// "").indexOf(".")))) {
		// tvOldVersion.setText("V" + AppUtil.getVersionCode(MoreActivity.this)
		// + ".0");
		// } else {
		// tvOldVersion.setText("V" + AppUtil.getVersionCode(MoreActivity.this)
		// + "");
		//
		// }
		tvOldVersion.setText("V" + AppUtil.getVersionName(MoreActivity.this));
		// if (updateInfo.forceUpdateCode < updateInfo.optionalUpdateCode) {
		// if ("-1".equals((updateInfo.optionalUpdateCode + "").indexOf("."))) {
		// tvHintUpdateTitle.setText("V" + updateInfo.optionalUpdateCode +
		// "版本更新信息");
		// tvHint.setText(updateInfo.updateInfo);
		// btnUpdate.setText("我要升级到版本" + "V" + updateInfo.optionalUpdateCode);
		// } else {
		// tvHintUpdateTitle.setText("V" + updateInfo.optionalUpdateCode + ".0"
		// + "版本更新信息");
		// tvHint.setText(updateInfo.updateInfo);
		// btnUpdate.setText("我要升级到版本" + "V" + updateInfo.optionalUpdateCode +
		// ".0");
		// }
		//
		// } else {
		// if ("-1".equals((updateInfo.forceUpdateCode + "").indexOf("."))) {
		// tvHintUpdateTitle.setText("V" + updateInfo.forceUpdateCode +
		// "版本更新信息");
		// tvHint.setText(updateInfo.updateInfo);
		// btnUpdate.setText("我要升级到版本" + "V" + updateInfo.forceUpdateCode);
		// } else {
		// tvHintUpdateTitle.setText("V" + updateInfo.forceUpdateCode + ".0" +
		// "版本更新信息");
		// tvHint.setText(updateInfo.updateInfo);
		// btnUpdate.setText("我要升级到版本" + "V" + updateInfo.forceUpdateCode +
		// ".0");
		// }
		// }

		tvHintUpdateTitle.setText("发现新版本，更新信息");
		tvHint.setText(updateInfo.updateInfo);
		btnUpdate.setText("我要升级到最新版本");

		btnUpdate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MoreActivity.this, UpdateSoftActivity.class);
				intent.putExtra("updateUrl", updateInfo.updateUrl);
				mContext.startActivity(intent);
				dialog.cancel();
			}
		});
		/*
		 * mBtnNextTime.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { dialog.cancel(); } });
		 * closeBtn.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { dialog.cancel(); } });
		 */
		dialog.show();
	}

	private void setParams(LayoutParams lay) {
		DisplayMetrics dm = new DisplayMetrics();
		mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
		Rect rect = new Rect();
		View view = mContext.getWindow().getDecorView();
		view.getWindowVisibleDisplayFrame(rect);
		lay.height = dm.heightPixels;
		lay.width = dm.widthPixels;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}
}
